package cn.st4rlight.util.excel.converter;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

import cn.st4rlight.util.constant.NumberConstant;

/**
 * 用于easy-excel的万分币 --> 百分比
 *
 * @author st4rlight <st4rlight@163.com>
 * Created on 2025-05-03
 */
public class TTHToPercentConverter implements Converter<Long> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Long.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Long> context) {
        Long value = context.getValue();
        if (Objects.isNull(value)) {
            new WriteCellData<>(CellDataTypeEnum.STRING, "-");
        }
        BigDecimal bigDecimal = new BigDecimal(value).setScale(2, RoundingMode.HALF_UP)
                .divide(NumberConstant.BIG_DECIMAL_100, RoundingMode.HALF_UP);
        return new WriteCellData<>(CellDataTypeEnum.STRING, bigDecimal.toPlainString() + "%");
    }
}
